/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.Ordinateur;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 *
 * @author Valentin
 */
public class OrdinateurDAO extends DAO<Ordinateur> {

    /**
     * Constructeur de la classe OrdinateurDAO
     *
     * @param conn
     */
    public OrdinateurDAO(Connection conn) {
        super(conn);
    }

    @Override
    public void insert(Ordinateur pc) throws SQLException {

        MaterielDAO myMaterielDAO = new MaterielDAO(connexion);
        myMaterielDAO.insert(pc);

        String query = " INSERT INTO"
                + " ordinateur"
                + " (numCB, proc, DD, RAM, CMere, CSon, CGraph, CRes, SE, nbFloppy, nbZip, nbCD, nbCDRW, nbDVD, nbDVDpRW, idE, nbDVDmRW, nbDVDpmRW)"
                + " VALUES (\""
                + pc.getNumCB() + ","
                + "\"" + pc.getProc() + "\","
                + "\"" + pc.getDd() + "\","
                + "\"" + pc.getRam() + "\","
                + "\"" + pc.getcMere() + "\","
                + "\"" + pc.getcSon() + "\","
                + "\"" + pc.getcGraph() + "\","
                + "\"" + pc.getcRes() + "\","
                + "\"" + pc.getSe() + "\","
                + pc.getNbFloppy() + ","
                + pc.getNbZip() + ","
                + pc.getNbCd() + ","
                + pc.getNbCdrw() + ","
                + pc.getNbDvd() + ","
                + pc.getNbDvdpRw() + ","
                + pc.getIde() + ","
                + pc.getNbDvdmRw() + ","
                + pc.getNbDvdpmRw() + ")";

        state.executeUpdate(query, state.RETURN_GENERATED_KEYS);
        /* try (ResultSet rs = state.getGeneratedKeys()) {
         ResultSetMetaData rsmd = rs.getMetaData();
         int columnCount = rsmd.getColumnCount();
         if (rs.next()) {
         do {
         for (int i = 1; i <= columnCount; i++) {
         int key = rs.getInt(i);
         embarcadere.setId(key);
         }
         } while (rs.next());
         } else {
         embarcadere.setId(-1);
         }
         }*/

    }

    @Override
    public void delete(Ordinateur pc) throws SQLException {

        MaterielDAO myMaterielDAO = new MaterielDAO(connexion);
        myMaterielDAO.delete(pc);

        String query = " DELETE FROM"
                + " ordinateur"
                + " WHERE numCB ="
                + pc.getNumCB();

        state.executeUpdate(query);
    }

    @Override
    public void update(Ordinateur pc) throws SQLException {
      
        MaterielDAO myMaterielDAO = new MaterielDAO(connexion);
        myMaterielDAO.update(pc);
  
        String query = " UPDATE  ordinateur"
                + " SET"
                + "  proc = \"" + pc.getProc() + "\""
                + " ,DD = \"" + pc.getDd() + "\""
                + " ,RAM = \"" + pc.getRam() + "\""
                + " ,CMere = \"" + pc.getcMere() + "\""
                + " ,CSon = \"" + pc.getcSon() + "\""
                + " ,CGraph = \"" + pc.getcGraph() + "\""
                + " ,CRes = \"" + pc.getcRes() + "\""
                + " ,SE = \"" + pc.getSe() + "\""
                + " ,nbFloppy = " + pc.getNbFloppy()
                + " ,nbZip = " + pc.getNbZip()
                + " ,nbCD = " + pc.getNbCd()
                + " ,nbCDRW = " + pc.getNbCdrw()
                + " ,nbDVD = " + pc.getNbDvd()
                + " ,nbDVDpRW = " + pc.getNbDvdpRw()
                + " ,idE = " + pc.getIde()
                + " ,nbDVDmRW = " + pc.getNbDvdmRw()
                + " ,nbDVDpmRW = " + pc.getNbDvdpmRw()
                + " WHERE numCB = " + pc.getNumCB();
        state.executeUpdate(query);
    }
}
